/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.dialog.io.view;

import java.io.File;
import javax.swing.JFileChooser;
import offset.nodes.client.dialog.io.model.BookType;

/**
 *
 * @author  Walter Lütgenau
 */
public class ExportBookPanel extends javax.swing.JPanel {

    JFileChooser chooser;
    String selectedFileName;

    /** Creates new form ExportPanel */
    public ExportBookPanel(JFileChooser chooser) {
        this.chooser = chooser;
        initComponents();
        updateFileName(null);
    }

    public int getRecursionLevel() {
        String recursionLevelString = (String) levelCombo.getSelectedItem();
        int recursionLevel = 0;
        try {
            recursionLevel = Integer.parseInt(recursionLevelString);
        } catch (NumberFormatException ne) {
            // cannot happen
        }

        return recursionLevel;
    }

    public BookType getType() {
        if (epubRadio.isSelected())
            return BookType.epub;
        else
            return BookType.html;
    }

    public boolean isFollowLinks() {
        return followLinksCheckbox.isSelected();
    }

    protected String getSelectedFileNameWithoutExtension() {
        if (selectedFileName == null) {
            if (chooser == null || chooser.getSelectedFile() == null)
                return null;

            selectedFileName = chooser.getSelectedFile().getName();
        }

        String fileName = selectedFileName;
        if (selectedFileName.indexOf(".") >= 0)
            fileName = fileName.substring(0, fileName.lastIndexOf("."));
        return fileName;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        typeButtonGroup = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        levelCombo = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        epubRadio = new javax.swing.JRadioButton();
        htmlRadio = new javax.swing.JRadioButton();
        followLinksCheckbox = new javax.swing.JCheckBox();

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("offset/nodes/client/dialog/view/DialogBundle"); // NOI18N
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("export.optionGroup"))); // NOI18N

        levelCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20" }));

        jLabel1.setText(bundle.getString("book")); // NOI18N

        typeButtonGroup.add(epubRadio);
        epubRadio.setSelected(true);
        epubRadio.setText(bundle.getString("book.type.epub")); // NOI18N
        epubRadio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                updateFileName(evt);
            }
        });

        typeButtonGroup.add(htmlRadio);
        htmlRadio.setText(bundle.getString("book.type.html")); // NOI18N
        htmlRadio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                updateFileName(evt);
            }
        });

        followLinksCheckbox.setText(bundle.getString("book.followLinks")); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(htmlRadio)
                            .addComponent(epubRadio))
                        .addGap(43, 43, 43)))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(levelCombo, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(followLinksCheckbox, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(epubRadio)
                    .addComponent(followLinksCheckbox))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(htmlRadio)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(levelCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(23, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void updateFileName(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_updateFileName
        String fileName = getSelectedFileNameWithoutExtension();
        if (fileName == null)
            return;

        if (epubRadio.isSelected())
            chooser.setSelectedFile(new File(fileName + ".epub"));
        else if (htmlRadio.isSelected())
            chooser.setSelectedFile(new File(fileName + ".zip"));
    }//GEN-LAST:event_updateFileName
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JRadioButton epubRadio;
    private javax.swing.JCheckBox followLinksCheckbox;
    private javax.swing.JRadioButton htmlRadio;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JComboBox levelCombo;
    private javax.swing.ButtonGroup typeButtonGroup;
    // End of variables declaration//GEN-END:variables
}
